<!doctype html>
<title>TextTrack.addCue()</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id=log></div>
<script>
setup(function(){
    window.video = document.createElement('video');
    document.body.appendChild(video);
}, {timeout:5000});
test(function() {
    var t1 = video.addTextTrack('subtitles');
    var t2 = video.addTextTrack('subtitles');
    var c1 = new VTTCue(0, 1, 'text1');
    t1.addCue(c1);
    t2.addCue(c1);
    assert_equals(c1.track, t2);
}, document.title+', adding a cue to two different tracks');
test(function() {
    var t1 = video.addTextTrack('subtitles');
    var c1 = new VTTCue(0, 1, 'text1');
    t1.addCue(c1);
    assert_equals(c1.track, t1);
    t1.addCue(c1);
    assert_equals(c1.track, t1);
}, document.title+', adding a cue to a track twice');
test(function() {
    var t1 = video.addTextTrack('subtitles');
    var t2 = video.addTextTrack('subtitles');
    var c1 = new VTTCue(0, 1, 'text1');
    t1.addCue(c1);
    assert_equals(c1.track, t1);
    t1.removeCue(c1);
    assert_equals(c1.track, null);
    t2.addCue(c1);
    assert_equals(c1.track, t2);
}, document.title+', adding a removed cue to a different track');
test(function() {
    var t1 = video.addTextTrack('subtitles');
    var c1 = new VTTCue(0, 1, 'text1');
    t1.addCue(c1);
    assert_equals(t1.cues.length, 1, 't1.cues.length after first addition');
    t1.removeCue(c1);
    assert_equals(t1.cues.length, 0, 't1.cues.length after removal');
    t1.addCue(c1);
    assert_equals(t1.cues.length, 1, 't1.cues.length after second addition');
}, document.title+', adding an associated but removed cue to the same track');

var t = async_test(document.title+', adding a cue associated with a track element to other track');
t.step(function(){
    var t1 = video.addTextTrack('subtitles');
    var track = document.createElement('track');
    track.onload = t.step_func(function(){
        var cue = track.track.cues[0];
        track.track.removeCue(cue);
        t1.addCue(cue);
        assert_equals(cue.track, t1);
        t.done();
    });
    track.onerror = t.step_func(function() {
      assert_unreached('got error event');
    });
    track.src= 'data:text/vtt,'+encodeURIComponent('WEBVTT\n\n00:00:00.000 --> 00:00:01.000\ntest\n');
    track.kind = 'subtitles';
    track.track.mode = 'hidden';
    video.appendChild(track);
});
</script>
